package com.tgy.algorithm.base;

import java.util.EmptyStackException;
import java.util.Stack;

// 使用两个栈实现队列的功能
class MyQueue {
    private Stack<Integer> firstStack = new Stack<Integer>();
    private Stack<Integer> secondStack = new Stack<>();
    MyQueue() {
    }

    // 1,2,3,4
    // 5,6
    // 添加元素
    public void offer(int value) {
        firstStack.push(value);
    }

    // 取出元素
    public int poll() {

        if (this.isEmpty()) {
            throw new EmptyStackException();
        }

        if(secondStack.isEmpty()) {
            while(!firstStack.isEmpty()) {
                secondStack.push(firstStack.pop());
            }
        }

        int val = secondStack.pop();
        return val;
    }

    public boolean isEmpty() {
        return firstStack.isEmpty() && secondStack.isEmpty();
    }

}

public class _栈实现队列 {
    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.poll());
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
        queue.offer(4);
//        System.out.println(queue.poll());
//        System.out.println(queue.poll());
        queue.offer(5);
        System.out.println("=======================");
        while(!queue.isEmpty()) {
            System.out.print(queue.poll()+" ");
        }
        System.out.println();
    }
}
